package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.MapUtils;
import io.renren.modules.project.dao.QulaityMarkScoreDao;
import io.renren.modules.project.entity.CheckQualityEntity;
import io.renren.modules.project.entity.QulaityMarkScoreEntity;
import io.renren.modules.project.service.CheckQualityService;
import io.renren.modules.project.service.QulaityMarkScoreService;
import io.renren.modules.project.vo.ScoreRequestVoEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.Map;
import java.util.List;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;



@Service("qulaityMarkScoreService")
public class QulaityMarkScoreServiceImpl extends ServiceImpl<QulaityMarkScoreDao, QulaityMarkScoreEntity> implements QulaityMarkScoreService {

    @Autowired
    private CheckQualityService checkQualityService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");

        Page<QulaityMarkScoreEntity> page = this.selectPage(
                new Query<QulaityMarkScoreEntity>(params).getPage(),
                new EntityWrapper<QulaityMarkScoreEntity>().like(StringUtils.isNotBlank(key),"id", key)
        );

        return new PageUtils(page);
    }

    @Override
    public List<QulaityMarkScoreEntity> queryList(Map<String, Object> params){
        List<QulaityMarkScoreEntity> list = this.selectList(
                new EntityWrapper<QulaityMarkScoreEntity>().eq("project_no",params.get("projectNo").toString())
        );
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveList(ScoreRequestVoEntity voEntity) {
        String projectNo = voEntity.getProjectNo();
        if(StringUtils.isNotBlank(projectNo)) this.deleteByMap(new MapUtils().put("project_no",projectNo));
        if(voEntity.getMarkList().size() > 0) this.insertBatch(voEntity.getMarkList());
        //根据项目编号获取质检检查
        CheckQualityEntity cqEntity = checkQualityService.selectOne(
                new EntityWrapper<CheckQualityEntity>().eq("project_no", voEntity.getProjectNo())
        );
        if(cqEntity == null) cqEntity =new CheckQualityEntity();
        cqEntity.setQualityScore(voEntity.getQualityScore());
        cqEntity.setProjectNo(voEntity.getProjectNo());
        checkQualityService.insertOrUpdate(cqEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(QulaityMarkScoreEntity entity) {
        this.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] Ids) {
        //删除
        this.deleteBatchIds(Arrays.asList(Ids));
    }

}